; ========================================================================
; TOGGLE STEPS/MM CALIBRATION
; ========================================================================
; Purpose: Enable or disable automatic steps/mm calibration from XY Auto Squaring
; This macro toggles the global.stepsCalibration variable and updates sys/user/actions/StepsCalibration.g
; Parameter R: If present, force enable mode (sets initial state to false, then toggles to true)
; ========================================================================

var filePathAction = "0:/sys/user/actions/StepsCalibration.g"
var filePathVariable = "0:/sys/user/variables/StepsCalibration.g"

; ===== Feature not yet available =====
if !exists(param.R)
  ; Write disabled state and default steps/mm to user file
  echo >{var.filePathVariable} "set global.stepsCalibration = false"
  echo >{var.filePathAction} "M92 X80 Y80 U80 Z400 E400:400  ; default steps/mm (calibration disabled) ; Default values - will be overwritten by Auto Calibration if enabled"
  
  M92 X80 Y80 U80 Z400 E400:400                              ; apply default steps/mm
  M291 S2 R"Feature Not Available" P"Unfortunately, this feature is not available yet.<br><br>It will be available in a future release."
  M99                                                        ; exit macro

; ===== Load global variables =====
M98 P{var.filePathVariable}                                  ; load current state

; ===== If parameter R exists, force initial state to false (will toggle to true) =====
if exists(param.R)
  set global.stepsCalibration = false

; ===== Toggle the calibration state =====
var newState = !global.stepsCalibration

; ===== Write updated state to file =====
echo >{var.filePathVariable} "set global.stepsCalibration = " ^ var.newState

if var.newState == false
  ; ===== Calibration DISABLED - Write default M92 command =====
  echo >{var.filePathAction} "M92 X80 Y80 U80 Z400 E400:400  ; default steps/mm (calibration disabled)"
  set global.stepsCalibration = false
  
  ; ===== Notify user that calibration is disabled (skip if parameter R) =====
  if !exists(param.R)
    M291 S1 T0 R"Steps/mm Calibration Disabled" P"Steps/mm calibration is now <b>DISABLED</b>.<br><br>Default values (X80 Y80 U80) will be used."
else
  ; ===== Calibration ENABLED - Write placeholder (will be updated by Auto Calibration) =====
  echo >{var.filePathAction} "M92 X80 Y80 U80 Z400 E400:400  ; default values - run Auto Calibration to update"
  set global.stepsCalibration = true
  
  ; ===== Prompt user to run Auto Calibration (skip if parameter R) =====
  if !exists(param.R)
    M291 S4 K{"Run Auto Calibration Now", "Skip for Now"} F0 R"Steps/mm Calibration Enabled" P"Steps/mm calibration is now <b>ENABLED</b>.<br><br>Run Auto Calibration to measure and apply corrected values."
    
    if input == 0
      ; User chose to run Auto Calibration now
      M98 P"0:/macros/Auto Calibration"

; ===== Reload the updated file to apply changes immediately =====
M98 P{var.filePathVariable}
M98 P{var.filePathAction}

echo "Steps/mm calibration is now " ^ {global.stepsCalibration ? "ENABLED" : "DISABLED"}
